﻿using Sasa.QualityTools.CodeAnalysis.Core.Models;
using Sasa.QualityTools.CodeAnalysis.Core.VisualStudio;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Sasa.QualityTools.CodeAnalysis.Core.Utilities
{
    public static class FxCopCmdOptionBuilder
    {
        public static string Build(ISolution solution)
        {
            if (solution == null)
            {
                throw new ArgumentNullException("solution");
            }

            var result = new List<string>();
            result.Add("/p:" + '"' + solution.FxCopProjectPath + '"');
            result.Add("/o:" + '"' + Path.Combine(solution.ConfigurationDir, Constants.CodeAnalysisReportFileName) + '"');
            result.Add("/sf");
            result.Add("/fo");

            if (File.Exists(solution.FxCopDictionaryPath))
            {
                result.Add("/dic:" + '"' + solution.FxCopDictionaryPath + '"');
            }

            return String.Join<string>(" ", result);
        }

        public static string Build(IProject project, Version version, params string[] typeNames)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }
            if (version == null)
            {
                throw new ArgumentNullException("version");
            }
            if (typeNames == null)
            {
                throw new ArgumentNullException("typeNames");
            }

            var config = CodeAnalysisConfiguration.Load(project.ConfigurationPath);
            config.Override(CodeAnalysisConfiguration.Load(project.Solution.ConfigurationPath));

            var result = new List<string>();

            result.Add("/f:" + '"' + project.AssemblyPath + '"');
            result.Add("/o:" + '"' + Path.Combine(project.TargetDir, Constants.CodeAnalysisReportFileName) + '"');
            result.Add("/c");
            result.Add("/sf");
            result.Add("/fo");
            result.Add("/to:" + ((config.Timeout > 0) ? Convert.ToInt32(config.Timeout) : 0));

            if (config.EnableSearchGac)
            {
                result.Add("/gac");
            }

            if (config.IgnoreGeneratedCode)
            {
                result.Add("/igc");
            }

            if (version.Major >= 10)
            {
                if (File.Exists(config.RuleSet))
                {
                    result.Add("/rs:=" + '"' + config.RuleSet + '"');
                    result.Add("/rsd:" + '"' + Path.GetFullPath(Path.Combine(Environment.GetEnvironmentVariable("VS100COMNTOOLS"), "..", "..", "Team Tools", "Static Analysis Tools", "Rule Sets")) + '"');

                }
            }

            if (File.Exists(project.Solution.FxCopDictionaryPath))
            {
                result.Add("/dic:" + '"' + project.Solution.FxCopDictionaryPath + '"');
            }

            foreach (var dir in project.DependencyDirectories)
            {
                result.Add("/d:" + '"' + dir.TrimEnd('\\') + '"');
            }

            if (typeNames.Length > 0)
            {
                result.Add("/t:" + String.Join(",", typeNames));
            }

            return String.Join<string>(" ", result);
        }
    }
}
